#pragma once
#include<assert.h>

namespace Aron
{
	template<class T>
	class verctor
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{}

		//v2(v1)
		vector(const vector<T>& v)
		{
			reserve(v.capacity());

			for (const auto& e : v)
			{
				push_back(e);
			}
		}

		
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}



		//v1=v3
		vector<T>& operator=(vector<T> v)
		{
			swap(v);

			return *this;
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}


		const_iterator end() const
		{
			return _finish;
		}


		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * old);
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + old;
				_endofstorage = _start + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _capacity)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
		}


		void pop_back()
		{
			assert(pos < size());

			_finish--;
		}

		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);

			if (_finish == _capacity)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}
			memmove(pos + 1, pos, sizeof(T) * (_finish - _pos));
			*pos = x;
			_finish++;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacioty() const
		{
			return _endofstorage - _start;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());

			return _start[pos];
		}


	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};


}